<?php
/**
 * Wax肤质检测数据的model
 * Author: lyun
 * Date: 2018/3/27
 * Created by Panxsoft.
 */


namespace App\Models\Store;

use App\Models\OpenWechat\WechatUser;
use App\Services\Utils\Bosable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Class WaxSkinRecord
 *
 * @property string itemsname
 * @property string cause
 * @property string expert
 * @property int value
 * @property int id
 * @property int itemsid
 * @property int levelsort
 *
 * @property-write string levelname
 *
 * @method static self filterWechatUserId(int $wx_user_id)
 *
 * @mixin Builder
 */
class WaxSkinRecord extends Model
{
    use Bosable;

    const ITEMSID_REDBLOOD = 1;   //  (红血丝) / 皮肤敏感度
    const ITEMSID_MOISTURE = 2;   //  (皮肤含水) / 皮肤含水
    const ITEMSID_OIL = 3;        //  (油脂)    / 油脂测试
    const ITEMSID_FOLLICLE = 4;       //  (黑头粉刺) / 毛孔清洁度
    const ITEMSID_PIGMENTATION = 5;  // (色素分解度值) / 美白祛斑

    // #898
    const ITEMSID_SORT = [
        self::ITEMSID_OIL          => 0,
        self::ITEMSID_MOISTURE     => 1,
        self::ITEMSID_REDBLOOD     => 2,
        self::ITEMSID_PIGMENTATION => 3,
        self::ITEMSID_FOLLICLE     => 4,
    ];

    const ITEMSID_FLEX = 6; //  （弹性纤维）/ 肌底抗衰度 - 未使用

    const ITEMSID_ALL = [
        self::ITEMSID_REDBLOOD,
        self::ITEMSID_MOISTURE,
        self::ITEMSID_OIL,
        self::ITEMSID_FOLLICLE,
        self::ITEMSID_PIGMENTATION
    ];

    const GOOD_LEVEL_SORT = 1;

    const ITEM_LOCALE_MAP = [
        'zh' => [
            self::ITEMSID_REDBLOOD     => '皮肤敏感度',
            self::ITEMSID_MOISTURE     => '水分测试',
            self::ITEMSID_OIL          => '油脂测试',
            self::ITEMSID_FOLLICLE     => '毛孔清洁度',
            self::ITEMSID_PIGMENTATION => '色素分解度',
        ],
        'en' => [
            self::ITEMSID_REDBLOOD     => 'sensitivity',
            self::ITEMSID_MOISTURE     => 'moisture',
            self::ITEMSID_OIL          => 'oil',
            self::ITEMSID_FOLLICLE     => 'cleanliness',
            self::ITEMSID_PIGMENTATION => 'pigment',
        ]
    ];

    protected $origin_img_bos_key_prefix = 'detector/wax_origin_img/';
    protected $analyse_img_bos_key_prefix = 'detector/wax_analyse_img/';

    protected $table = 'wax_skin_records';
    
    protected $guarded = [];

    protected $casts = [
        'value'          => 'int',
        'itemsid'        => 'int',
        'wechat_user_id' => 'int',
        'skin_record_id' => 'int',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function wechatUser()
    {
       return $this->belongsTo(WechatUser::class, 'wechat_user_id', 'id');
    }

    /**
     * @used-by filterWechatUserId
     * @param Builder $query
     * @param $openid
     * @return Builder
     */
    public function scopeFilterWechatUserId(Builder $query, $openid)
    {
        if (!$openid) return $query;

        return $query->whereHas('wechatUser', function(BelongsTo $query) use($openid) {
            $query->where('openid', $openid);
        });
    }

    public function skinRecord(): BelongsTo
    {
        return $this->belongsTo(SkinRecord::class);
    }

    /**
     * @param array $skin_record_wax
     * @return mixed
     */
    public static function checkHasSkinRecord(array $skin_record_wax)
    {
        $wax_skin_record = (new self)
            ->where('skin_record_id', $skin_record_wax['skin_record_id'])
            ->where('itemsid', $skin_record_wax['itemsid'])
            ->first();

        return $wax_skin_record;
    }

    /**
     * @param $origin_img
     * @throws \Exception
     */
    public function setOriginImgAttribute($origin_img)
    {
        if (is_file($origin_img)) {
            $this->attributes['origin_img'] = $this->uploadPostFile($origin_img , $this->origin_img_bos_key_prefix);
        } elseif (is_string($origin_img)) {
            $this->attributes['origin_img'] = $origin_img;
        }
    }

    /**
     * @param $analyse_img
     * @throws \Exception
     */
    public function setAnalyseImgAttribute($analyse_img)
    {
        if (is_file($analyse_img)) {
            $this->attributes['analyse_img'] = $this->uploadPostFile($analyse_img , $this->analyse_img_bos_key_prefix);
        } elseif (is_string($analyse_img)) {
            $this->attributes['analyse_img'] = $analyse_img;
        }
    }

    /**
     * @return mixed|null
     */
    public function getOriginImgAttribute()
    {
        if (! $this->attributes['origin_img']) return null;

        return $this->getStaticUrl($this->attributes['origin_img']);
    }

    /**
     * @return mixed|null
     */
    public function getAnalyseImgAttribute()
    {
        if (! $this->attributes['analyse_img']) return null;

        return $this->getStaticUrl($this->attributes['analyse_img']);
    }
}
